//
//  PCDApiReLaunch.m
//  PCDBank
//
//  Created by DK on 2021/2/25.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDApiReLaunch.h"
#import "PCDBankWebViewController.h"

@implementation PCDApiReLaunch
- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback
{
    //
    UIViewController *viewPage = context.viewController;
    NSDictionary *tempDic = (NSDictionary *)data;
    NSString *callBack = [tempDic objectForKey:@"callback"];
    NSString *strUrl = [tempDic objectForKey:@"url"];
    
    //packageCode bid 离线包编号
    NSString *appId = [tempDic objectForKey:@"appId"];
    
    //应用编号，appId
    NSString *appName = [tempDic objectForKey:@"appName"];
    
    NSString *strFrameworkType = [tempDic objectForKey:@"frameworkType"];
    NSString *extraData =[tempDic objectForKey:@"extraData"];
    
    if ([strUrl hasPrefix:@"/"]) {
        strUrl = [strUrl substringFromIndex:1];
    }
    
    strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    if([PCDPageViewMangeGet().appId isEqual:appId]) {
        
        NSDictionary *params = @{@"curl":strUrl,@"appId":appId};
        
        UIViewController *viewController=[PCDPageViewMangeGet() creatCacheWebController:params View:nil Status:NO];
        //清理所有栈内页面
        NSMutableArray *viewCtrs = [self  clearViews];
        [viewCtrs addObject:viewController];
    
        [PCDPageViewMangeGet().baseNavgatView setViewControllers:viewCtrs animated:NO];
    
        if (![PCDUtil nilOrEmptyString:callBack]) {
            //
            evaluateJavaScriptOnMainThread(self,callBack,@"");
        }
        
    }else{
        
        
        if ([PCDUtil nilOrEmptyString:strFrameworkType]) {
            strFrameworkType = @"6";
        }
        if ([PCDUtil nilOrEmptyString:appName]) {
            appName =[PCDUtil channelId];
        }
        
        NSDictionary *params = @{@"bid":[PCDUtil safeString:appId],@"url":[PCDUtil safeString:strUrl],@"frameworkType":strFrameworkType,@"appId":[PCDUtil safeString:appName]};
        
        if (!PCDPageViewMangeGet().bOnLine) {
            [[PCDLoadPackageManage sharedService] loadSixPackageWithBid:params CompletionBlock:^(id  _Nonnull object, BOOL success, NSString * _Nonnull error) {
                //
                if (success) {

                    dispatch_async(dispatch_get_main_queue(), ^{

                        NSDictionary *tempDic = (NSDictionary *)object;

                        UIViewController *viewController=[PCDPageViewMangeGet() creatCacheWebController:tempDic View:nil Status:NO];
                        //清理所有栈内页面
                        NSMutableArray *viewCtrs = [self clearViews];
                        [viewCtrs addObject:viewController];
                    
                        [PCDPageViewMangeGet().baseNavgatView setViewControllers:viewCtrs animated:NO];
                    
                        if (![PCDUtil nilOrEmptyString:callBack]) {
                            //
                            evaluateJavaScriptOnMainThread(self,callBack,@"");
                        }
                       
                    });
                }else{
                    [viewPage.view makeToast:error];
                }
            }];
        }else{
            
            NSDictionary *params = @{@"curl":strUrl};
        
            UIViewController *viewController=[PCDPageViewMangeGet() creatCacheWebController:params View:nil Status:NO];
        
            //清理所有栈内页面
            NSMutableArray *viewCtrs = [self clearViews];
            [viewCtrs addObject:viewController];
        
            [PCDPageViewMangeGet().baseNavgatView setViewControllers:viewCtrs animated:NO];
        
            if (![PCDUtil nilOrEmptyString:callBack]) {
                //
                evaluateJavaScriptOnMainThread(self,callBack,@"");
            }

        }
        
    }

}

-(NSMutableArray *)clearViews
{
    NSMutableArray *viewCtrs = [NSMutableArray arrayWithArray:PCDPageViewMangeGet().baseNavgatView.viewControllers];
    
    int i = viewCtrs.count-1;
    while (i>=0) {
        
        id view = [viewCtrs objectAtIndex:i];
        
        if ([view isKindOfClass:[PCDBankWebViewController class]]) {
            
//            PCDBankWebViewController *viewPage = (PCDBankWebViewController *)view;
            [viewCtrs removeObjectAtIndex:i];
            i--;
        }else{
            break;
        }

    }
    
    return viewCtrs;
}

@end
